package com.yishun.springboot.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArratUtils {

	/**
	 * @Description: List的工具类
	 * @Author JiaYun
	 * @Date 2021/7/14 15:53
	 * @Version V1.0
	 **/

	/**
	 * @Author JiaYun
	 * @Description 比较两个list的差异
	 * @param preList 旧list
	 * @param curList 新list
	 * @Return java.util.List<java.util.List<T>>
	 *         第一个List是preList独有的元素集合，第二个List是curList独有的元素集合
	 * @Date 2021/7/14 16:17
	 */
	public static <T> List<List<T>> compare(List<T> preList, List<T> curList) {

		Map<T, Boolean> map = new HashMap<>();
		for (T p : preList) {
			map.put(p, false);
		}

		List<T> curAddList = new ArrayList<>();
		for (T c : curList) {
			if (map.containsKey(c)) {
				map.put(c, true);
				continue;
			} else {
				curAddList.add(c);
			}
		}

		List<T> preSurplusList = new ArrayList<>();
		for (Map.Entry<T, Boolean> entry : map.entrySet()) {
			if (!entry.getValue()) {
				preSurplusList.add(entry.getKey());
			}
		}

		List<List<T>> res = new ArrayList<>();
		res.add(preSurplusList);
		res.add(curAddList);

		return res;
	}
}
